package com.student.realm;


import com.student.entity.User;
import com.student.service.ManageService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * Created by zhudong on 2016/8/17.
 */
@Repository("realm")
public class UserRealm extends AuthorizingRealm {

    @Autowired
    private ManageService manageService;

    /*@Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        String username = (String)principals.getPrimaryPrincipal();
        Set<String> roleNames = roleService.getRoleNamesByUsername(username);
        Set<String> authorizationNames = authorizationService.getAuthorizationNamesByUsername(username);
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.addRoles(roleNames);
        simpleAuthorizationInfo.addStringPermissions(authorizationNames);
        return simpleAuthorizationInfo;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String username = (String)token.getPrincipal();
        String password = new String((char[])token.getCredentials());

        if(!userService.login(username,password)){
            throw new UnknownAccountException("用户名或者密码错误");
        }
        return new SimpleAuthenticationInfo(username,password,getName());
    }*/

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        String username = (String)principals.getPrimaryPrincipal();
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        authorizationInfo.setRoles(manageService.findRolesByUsername(username));
        authorizationInfo.setStringPermissions(manageService.findPermissionsByUsername(username));
        return authorizationInfo;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String username = (String)token.getPrincipal();
        System.out.printf("test");
        String password = new String((char[])token.getCredentials());
        User user = manageService.getUserByUsername(username);//测试
//        User user = new User();
//        user.setUsername("admin");
//        user.setPassword("123456");
        if(user == null) {
            throw new UnknownAccountException("找不到该用户");//没找到帐号
            //还有可能出现账号锁定等情况
        }
        if(password.equals(user.getPassword())){
            SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
                    user.getUsername(),
                    user.getPassword(),
                    getName()
            );
            return authenticationInfo;
        } else {
            throw new UnknownAccountException("用户名或者密码错误");
        }
    }
}
